package com.boot.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.boot.project.domain.UserDO;
import com.boot.project.domain.UserInfoDo;
import com.boot.project.service.UserService;
import com.boot.project.wechat.applet.service.UserConstantInterface;
import com.boot.project.wechat.applet.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userApi")
public class UserController {

    @Autowired
    private UserService userService;


    // 登录
    @PostMapping("/me/login")
    public Map<String, Object> user_login(@RequestBody JSONObject params) {
        String code = params.getString("code");
        System.out.println(code);
        String userHead = params.getString("userHead");
        String userName = params.getString("userName");
        String userGender = params.getString("userGender");
        String userCity = params.getString("userCity");
        String userProvince = params.getString("userProvince");

        // 配置请求参数
        Map<String, String> param = new HashMap<>();
        param.put("appid", UserConstantInterface.WX_LOGIN_APPID);
        param.put("secret", UserConstantInterface.WX_LOGIN_SECRET);
        param.put("js_code", code);
        param.put("grant_type", UserConstantInterface.WX_LOGIN_GRANT_TYPE);
        // 发送请求
        String wxResult = HttpClientUtil.doGet(UserConstantInterface.WX_LOGIN_URL, param);
        System.out.println(wxResult+"????????????????????????????");
        JSONObject jsonObject = JSONObject.parseObject(wxResult);
        // 获取参数返回的
        String session_key = jsonObject.get("session_key").toString();
        String open_id = jsonObject.get("openid").toString();
        Long userId = userService.getIdByOpenId(open_id);
        Integer workUser = userService.getWorkUser(open_id);
        // 根据返回的user实体类，判断用户是否是新用户，不是的话，更新最新登录时间，是的话，将用户信息存到数据库
        UserDO user = userService.selectByOpenId(open_id);
        if(user != null){
            user.setUserNewLogin(new Date());
            userService.updateById(user);
        }else{
            UserDO insert_user = new UserDO();
            insert_user.setUserHead(userHead);
            insert_user.setUserName(userName);
            insert_user.setUserGender(userGender);
            insert_user.setUserNewLogin(new Date());
            insert_user.setUserCity(userCity);
            insert_user.setUserProvince(userProvince);
            insert_user.setUserOpenId(open_id);
            System.out.println("insert_user:"+insert_user.toString());
            // 添加到数据库
            Boolean flag = userService.insert(insert_user);
            if(!flag){
                Map<String, Object> errorMap = new HashMap<>();
                errorMap.put("msg", "失败");
                return errorMap;
            }
        }
        // 封装返回小程序
        Map<String, Object> result = new HashMap<>();
        result.put("session_key", session_key);
        result.put("open_id", open_id);
        result.put("userId", userId);
        result.put("workUser", workUser);
        return result;

    }

    // 获取某个用户的个人资料
    @GetMapping("/userInfo")
    public UserInfoDo userInfo(@RequestParam String openId){
        return userService.userInfo(openId);
    }

    // 用户修改或新增个人资料
    @PostMapping("/updateUserInfo")
    public Map<String, Object> updateUserInfo(@RequestBody UserInfoDo userInfoDo){
        return userService.updateUserInfo(userInfoDo);
    }

    // 修改用户积分接口
    @PutMapping("/updateIntegral")
    public Map<String, Object> updateIntegral(@RequestParam Long userId, Double integral){
        return userService.updateIntegral(userId, integral);
    }

    // 获取所有用户的接口
    @GetMapping("/allUsers")
    public List<UserDO> allUsers(){
        return userService.allUsers();
    }

    // 获取用户剩余余额和剩余积分接口
    @GetMapping("/userBalanceAndIntegral")
    public UserDO userBalanceAndIntegral(@RequestParam String openId){
        return userService.userBalanceAndIntegral(openId);
    }

    // 保存用户手机号
    @GetMapping("/saveMobile")
    public Map<String, Object> saveMobile(@RequestParam Long mobile, String openId){
        return userService.saveMobile(mobile, openId);
    }

}